Imports System.Windows.Forms
Imports Microsoft.Win32

Friend Class frmOptionsDialog
    Private cReg As New libRegistry
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmOptionsDialog_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'My.Settings.Save()
    End Sub

    Private Sub OptionsDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fItem As States.comboBoxItem = Nothing

        cbDispSpeed.Items.Clear()
        cbType.Items.Clear()
        cbLog.Items.Clear()
        For i As UInt32 = 1 To 20
            cbDispSpeed.Items.Add(Str(i))
        Next
        If My.Settings.DevMode Then
            cbType.Items.AddRange(New Object() {New States.comboBoxItem("Integrated", 1), New States.comboBoxItem("Raw", 2), New States.comboBoxItem("Test", 3)})
        Else
            cbType.Items.AddRange(New Object() {New States.comboBoxItem("Integrated", 1), New States.comboBoxItem("Raw", 2)})
        End If
        For i As Integer = 0 To 20
            cbLog.Items.Add(Str(i * 400))
        Next


        cbAuto.Checked = My.Settings.stream_auto
        cbLPT.SelectedText = GetRegistryKey("emulator_lpt", 0)
        cbDispSpeed.SelectedItem = Str(My.Settings.stream_delay)
        cbLog.SelectedItem = Str(My.Settings.stream_trigger)
        For Each item As Object In cbType.Items
            fItem = CType(item, States.comboBoxItem)
            If fItem.Value = GetRegistryKey("stream_type", 2) Then
                cbType.SelectedItem = item
            End If
        Next

        If My.Settings.stream_Imperial Then
            cbMeasure.SelectedItem = "Imperial"
        Else
            cbMeasure.SelectedItem = "Metric"
        End If
    End Sub

    Private Sub cbDispSpeed_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDispSpeed.SelectedIndexChanged
        If cbDispSpeed.SelectedItem <> "" Then My.Settings.stream_delay = cbDispSpeed.SelectedItem 'SetRegistryKey("stream_delay", cbDispSpeed.SelectedItem)
    End Sub
    Private Sub cbLPT_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLPT.SelectedIndexChanged
        If cbLPT.SelectedItem <> "" Then My.Settings.Emu_Lpt = cbLPT.SelectedItem 'SetRegistryKey("emulator_lpt", cbLPT.SelectedItem)
    End Sub
    Private Sub cbLog_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLog.SelectedIndexChanged
        If cbLog.SelectedItem <> "" Then My.Settings.stream_trigger = cbLog.SelectedItem 'SetRegistryKey("stream_trigger", cbLog.SelectedItem)
    End Sub
    Private Sub cbType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbType.SelectedIndexChanged
        If Not IsNothing(cbType.SelectedItem) Then SetRegistryKey("stream_type", CType(cbType.SelectedItem, States.comboBoxItem).Value)
    End Sub

    Private Sub clrEmulator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrEmulator.Click
        colourSelect.Color = My.Settings.EmuTraceColour
        If colourSelect.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Settings.EmuTraceColour = colourSelect.Color
        End If
    End Sub
    Private Sub clrConsult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrConsult.Click
        colourSelect.Color = My.Settings.TraceColour
        If colourSelect.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Settings.TraceColour = colourSelect.Color
        End If
    End Sub
    Private Sub btnSaveDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveDisplay.Click
        My.Settings.form_Layout = frmBikiMain.DisplayByte
    End Sub

    Private Sub cbMeasure_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbMeasure.SelectedIndexChanged
        Try
            If cbMeasure.SelectedItem = "Imperial" Then
                My.Settings.stream_Imperial = True
            ElseIf cbMeasure.SelectedItem = "Metric" Then
                My.Settings.stream_Imperial = False
            End If
        Catch
        End Try
    End Sub

    Private Sub btnGraphBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraphBack.Click
        colourSelect.Color = My.Settings.Graph_BackColour
        If colourSelect.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Settings.Graph_BackColour = colourSelect.Color
        End If
    End Sub

    Private Sub btnGraphGradiant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraphGradiant.Click
        colourSelect.Color = My.Settings.Graph_GradiantColour
        If colourSelect.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Settings.Graph_GradiantColour = colourSelect.Color
        End If
    End Sub

    Private Sub cbAuto_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAuto.CheckedChanged
        My.Settings.stream_auto = cbAuto.Checked
    End Sub

    Private Sub btnSetUsb11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetUsb11.Click
        close_bikirom()
        If BikiRom_connect() = BK_return.BK_OK Then
            Dim ftData As FT_PROGRAM_DATA
            FT_EE_Read(lngHandle, ftData)

            ftData.MaxPower = 450


            ' FT_EE_Program(lngHandle, ftData)
        End If
    End Sub
End Class
